

import org.apache.pdfbox.io.RandomAccessFile;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.*;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        convert("D:\\idea\\pdf2.0\\src\\main\\resources\\book.pdf");
////        String arg = args[0];
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("请输入存储原始pdf文件夹路径(别换行)：");
//        String arg = scanner.nextLine();
////        String arg ="D:\\ideaprojects\\ppddff";
//        String[] split = arg.split(File.separator);
//        String mkdirName = split[0];
//        File file = new File(mkdirName+"\\result");
//        if (!file.exists()){
//            file.mkdirs();
//        }
//
//        List<String> list = allFile(arg);
//        for (String s : list){
//           pdfConvertToTxt(s,file.toString());
//        }
//        String path  ="D:\\test\\1.pdf";
//        convert(path);
    }
    public static List<String>  allFile(String path){
        List list = new ArrayList();
        File f = new File(path);
        // 如果文件f为一个目录文件
        if (f.isDirectory()){
            // 得到目录中的所有文件索引
            File[] fs = f.listFiles();
            // 如果目录中不为空
            if(fs != null && fs.length > 0){
                // 递归打印其中的所有文件路径
                for (File a: fs ) {
                    if(a.isDirectory()){
                        allFile(a.getPath());
                    }else{
                        list.add(a.toString());
                    }
                }
            }
        }
        return list;
    }
    public static void pdfConvertToTxt(String fileName,String path){

        String string = null;
        String separator = File.separator+"\\";
        FileOutputStream fileOutputStream = null;
        try {
            PDFParser pdfParser = new PDFParser(new RandomAccessFile(new File(fileName),"r"));
            pdfParser.parse();
            PDDocument pdDocument = new PDDocument(pdfParser.getDocument());
            PDFTextStripper stripper = new CustomTextStripper();
            string = stripper.getText(pdDocument);
            String[] split = fileName.split(separator);
            String target = split[split.length-1];
            String[] split1 = target.split(separator+".");
            String s = split1[0];
            String path1 = path+separator+s+".txt";
            fileOutputStream = new FileOutputStream(path1);
            fileOutputStream.write(string.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
    public static void convert(String filepath){
        String separator = File.separator+"\\";
        String[] split = filepath.split(separator);
        String mkdirName = split[0];
        File file = new File(mkdirName+separator+"result");
        if (!file.exists()){
            file.mkdirs();
        }
        pdfConvertToTxt(filepath,file.toString());





    }

}
